<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/app.php');

need_manager();

$action = strval($_GET['action']);
$id = abs(intval($_GET['id']));

/* 项目审核 business start */
if ( 'pass' == $action ) {
	$team = Table::Fetch('team', $id);
	if (!$team) json('无数据', 'alert');
	
	Table::UpdateCache('team', $id, array(
		'team_type' => 'normal',
		'business_audit' => 'T',
		'business_display' => 'Y',
	));
	Session::Set('notice', "审核通过");
	json(null, 'refresh');
}
elseif ( 'nopass' == $action ) {
	$team = Table::Fetch('team', $id);
	if (!$team) json('无数据', 'alert');
	
	Table::UpdateCache('team', $id, array(
		'team_type' => 'business',
		'business_audit' => 'F',
		'business_display' => 'N',
	));
	Session::Set('notice', "审核未通过");
	json(null, 'refresh');
}
elseif ( 'unpass' == $action ) {
	$team = Table::Fetch('team', $id);
	if (!$team) json('无数据', 'alert');
	
	Table::UpdateCache('team', $id, array(
		'team_type' => 'business',
		'business_audit' => 'D',
		'business_display' => 'N',
	));
	Session::Set('notice', "审核关闭");
	json(null, 'refresh');
}
else if ( 'teamdetail' == $action) {
	$team = Table::Fetch('team', $id);
	$partner = Table::Fetch('partner', $team['partner_id']);

	$paycount = Table::Count('order', array(
		'state' => 'pay',
		'team_id' => $id,
	));
	$buycount = Table::Count('order', array(
		'state' => 'pay',
		'team_id' => $id,
	), 'quantity');
	$onlinepay = Table::Count('order', array(
		'state' => 'pay',
		'team_id' => $id,
	), 'money');
	$creditpay = Table::Count('order', array(
		'state' => 'pay',
		'team_id' => $id,
	), 'credit');
	$cardpay = Table::Count('order', array(
		'state' => 'pay',
		'team_id' => $id,
	), 'card');
	$couponcount = Table::Count('coupon', array(
		'team_id' => $id,
	));
	$team['state'] = team_state($team);
	$city_id = abs(intval($team['city_id']));
	$subcond = array(); if($city_id) $subcond['city_id'] = $city_id;
	$subcount = Table::Count('subscribe', $subcond);
	$subcond['enable'] = 'Y';
	$smssubcount = Table::Count('smssubscribe', $subcond);

	/* send team subscribe mail */	
	$team['noticesubscribe'] = ($team['close_time']==0&&is_manager());
	$team['noticesmssubscribe'] = ($team['close_time']==0&&is_manager());
	/* send success coupon */
	$team['noticesms'] = ($team['delivery']!='express')&&(in_array($team['state'], array('success', 'soldout')))&&is_manager();
	/* teamcoupon */
	$team['teamcoupon'] = ($team['noticesms']&&$buycount>$couponcount);
	$team['needline'] = ($team['noticesms']||$team['noticesubscribe']||$team['teamcoupon']);

	/* 处理多城市id */
	$team['city_ids'] = array_filter(explode('@', $team['city_ids']));

	$html = render('manage_ajax_dialog_teamdetail');
	json($html, 'dialog');
}
else if ( 'teamremove' == $action) {
	need_auth('team');
	$team = Table::Fetch('team', $id);
	$order_count = Table::Count('order', array(
		'team_id' => $id,
		'state' => 'pay',
	));
	if ( $order_count > 0 ) {
		json('本团购包含付款订单，不能删除', 'alert');
	}
	ZTeam::DeleteTeam($id);

	/* remove coupon */
	$coupons = Table::Fetch('coupon', array($id), 'team_id');
	foreach($coupons AS $one) Table::Delete('coupon', $one['id']);
	/* remove order */
	$orders = Table::Fetch('order', array($id), 'team_id');
	foreach($orders AS $one) Table::Delete('order', $one['id']);
	/* end */

	Session::Set('notice', "团购 {$id} 删除成功！");
	json(null, 'refresh');
}
/* 项目审核 business end */
/* 业务员审核 business start */
elseif ( 'through' == $action ) {
	$team = Table::Fetch('user', $id);
	if (!$team) json('无数据', 'alert');
	
	Table::UpdateCache('user', $id, array(
		'business_admin' => 'Y',
	));
	Session::Set('notice', "成功设置业务员管理权限！");
	json(null, 'refresh');
}
elseif ( 'unthrough' == $action ) {
	$team = Table::Fetch('user', $id);
	if (!$team) json('无数据', 'alert');
	
	Table::UpdateCache('user', $id, array(
		'business_admin' => 'N',
	));
	Session::Set('notice', "取消业务员管理权限");
	json(null, 'refresh');
}
elseif ( 'delete' == $action ) {
	$opuser = Table::Fetch('user', $id);
	if ('Y'==$opuser['manager']) json('不能删除管理员用户', 'alert');
	if (0<$opuser['money']) json('用户余额大于0，不能删除', 'alert');
	$corder = Table::Count('order', array(
		'user_id' => $id,
		'state' => 'pay',
	));
	if (0<$corder) json('用户已经产生付款订单，不能删除', 'alert');
	Table::Delete('user', $id);
	Table::Delete('order', $id, 'user_id');
	Table::Delete('flow', $id, 'user_id');
	Table::Delete('coupon', $id, 'user_id');
	Table::Delete('ask', $id, 'user_id');
	Table::Delete('topic', $id, 'user_id');
        Table::Delete('toolsbind', $id, 'user_id');
	Session::Set('notice', '删除用户成功');
	json(null, 'refresh');
}
/* 业务员审核 business start */
/* 业务员审核 business en */
/* 商家审核 business start */
elseif ( 'partnerpass' == $action ) {
	$partner = Table::Fetch('partner', $id);
	if (!$partner) json('无数据', 'alert');
	
	Table::UpdateCache('partner', $id, array(
		'business_display' => 'Y',
	));
	Session::Set('notice', "审核通过");
	json(null, 'refresh');
}